package de.gwtprime.client.util;

import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.Window;
import com.google.web.bindery.event.shared.UmbrellaException;

/**
 * Show server errors to the user
 */
public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {

	@Override
	public void onUncaughtException( Throwable e ) {
		// Get rid of UmbrellaException
		Throwable exceptionToDisplay = getExceptionToDisplay( e );
		// Replace with your favorite message dialog, e.g. GXT's MessageBox
		Window.alert( exceptionToDisplay.getMessage() );
	}

	private static Throwable getExceptionToDisplay( Throwable throwable ) {
		Throwable result = throwable;
		if (throwable instanceof UmbrellaException && ((UmbrellaException) throwable).getCauses().size() == 1) {
			result = ((UmbrellaException) throwable).getCauses().iterator().next();
		}
		return result;
	}
}